﻿using System;
using System.Collections.Generic;
using System.Text;
using FIRADriverLibrary;
using Geometry;
using System.Drawing;

namespace PotentialFieldDriver.RobAgentSoccer
{
    class CoachAgent
    {
        private FIRADriver firaDriver;
        private RectangleF pitchArea;
        private double goalWidth;
        private List<FIRARobotState> players = new List<FIRARobotState>();
        private List<FIRARobotState> oponents = new List<FIRARobotState>();

        private RobAgentAtack agentAtack;
        private RobAgentCentral agentCentral;
        private RobAgentDefence agentDefence;
        private RobAgentGoalKeeper agentGoalKeeper;
        private List<IRobAgent> agents = new List<IRobAgent>();

        protected GameState gameState = GameState.Off;

        public GameState GameState
        {
            get { return gameState; }
            set
            {
                gameState = value;
                foreach (IRobAgent agent in agents)
                {
                    agent.GameState = value;
                }
            }
        }

        public CoachAgent(FIRADriver firaDriver, RectangleF pitchArea, double goalWidth)
        {
            this.firaDriver = firaDriver;
            this.pitchArea = pitchArea;
            this.goalWidth = goalWidth;

            foreach (FIRARobotState robot in firaDriver.firaRobots)
            {
                if (robot.owned)
                {
                    players.Add(robot);
                }
                else
                {
                    oponents.Add(robot);
                }
            }

            if (players.Count != 5 || oponents.Count != 5)
            {
                throw new Exception("Wrong size of teams");
            }

            agentAtack = new RobAgentAtack();
            agentAtack.addRobot(players[0]);
            agents.Add(agentAtack);

            agentCentral = new RobAgentCentral();
            agentCentral.addRobot(players[1]);
            agentCentral.addRobot(players[2]);
            agents.Add(agentCentral);

            agentDefence = new RobAgentDefence();
            agentDefence.addRobot(players[3]);
            agents.Add(agentDefence);

            agentGoalKeeper = new RobAgentGoalKeeper();
            agentGoalKeeper.addRobot(players[4]);
            agents.Add(agentGoalKeeper);

            foreach (IRobAgent agent in agents)
            {
                agent.Setup(firaDriver, pitchArea, goalWidth);
            }
        }

        public void Refresh()
        {
            agentAtack.Refresh();
            agentCentral.Refresh();
            agentDefence.Refresh();
            agentGoalKeeper.Refresh();
        }
    }
}
